package com.lihao.wanandroid.ui.navigation

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView.VERTICAL
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.kingja.loadsir.core.LoadService
import com.lihao.jetpackcore.ext.nav
import com.lihao.jetpackcore.ext.parseState
import com.lihao.wanandroid.R
import com.lihao.wanandroid.app.base.BaseFragment
import com.lihao.wanandroid.app.ext.LoadServiceInit
import com.lihao.wanandroid.app.ext.init
import com.lihao.wanandroid.app.ext.showError
import com.lihao.wanandroid.app.ext.showLoading
import kotlinx.android.synthetic.main.fragment_navigation.*
import kotlinx.android.synthetic.main.fragment_navigation.recyclerView
import timber.log.Timber

/**
 * Created by lihao on 2020/5/19.
 * Describe: 导航Fragment
 */
class NavigationFragment : BaseFragment<NavigationViewModel>() {

    private lateinit var loadSir: LoadService<Any>

    val mAdapter: NaviTitleAdapter by lazy {
        NaviTitleAdapter().apply {
            setOnItemClickListener { adapter, view, position ->
                mAdapter.setSelected(position)
                viewPager.setCurrentItem(position, false)
            }
        }
    }

    var fragments = arrayListOf<Fragment>()

    override fun layoutId() = R.layout.fragment_navigation

    override fun initView(savedInstanceState: Bundle?) {
        loadSir = LoadServiceInit(lineaLayout) {
            loadSir.showLoading()
            mViewModel.getNaviData()
        }
        initTitle("导航", isShowBack = false)
        recyclerView.init(
            LinearLayoutManager(context, VERTICAL, false), mAdapter
        )
        viewPager.run {
            isUserInputEnabled = false
            orientation = ViewPager2.ORIENTATION_VERTICAL
            adapter = object : FragmentStateAdapter(this@NavigationFragment) {
                override fun getItemCount() = fragments.size
                override fun createFragment(position: Int) = fragments[position]
            }
        }
    }

    override fun lazyLoadData() {
        loadSir.showLoading()
        mViewModel.getNaviData()
    }

    override fun createObserver() {
        mViewModel.naviLiveData.observe(this, Observer { resultState ->
            parseState(resultState, {
                loadSir.showSuccess()
                if (it.size > 0) {
                    it[0].isSelected = true
                    mAdapter.setList(it)
                    fragments.clear()
                    for (bean in it) {
                        fragments.add(NavListFragment.createInstant(bean))
                    }
                    viewPager.adapter?.notifyDataSetChanged()
                }
            }, {
                loadSir.showError(it.errorMsg)
            })
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        Timber.d("onDestroy")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        Timber.d("onDestroyView")
    }

    override fun onDetach() {
        super.onDetach()
        Timber.d("onDetach")
    }

}